package com.wc.acwing.动态规划.线性DP.最长公共上升子序列;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/15 23:53
 * @description https://www.acwing.com/problem/content/description/274/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int n;
    static int N = 3000 + 7;
    static int[][] f = new int[N][N];
    static long[] a = new long[N];
    static long[] b = new long[N];

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextLong();
        }
        for (int i = 1; i <= n; i++) {
            b[i] = sc.nextLong();
        }
        for (int i = 1; i <= n; i++) {
            int maxV = 1;
            for (int j = 1; j <= n; j++) {
                f[i][j] = f[i - 1][j];
                if (b[j] < a[i]) {
                    maxV = Math.max(maxV, f[i - 1][j] + 1);
                }
                if (a[i] == b[j]) {
                    f[i][j] = Math.max(f[i][j], maxV);
                }
            }
        }
        // 还得遍历是谁结尾的时候是最大的
        int ans = 0;
        for (int i = 1; i <= n; i++) {
            ans = Math.max(ans, f[n][i]);
        }
        out.println(ans);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否有下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
